package web

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"iris_blog/utils"
)

type Nav struct {
}

// 首页的分页列表
func (a Nav) GetAll(i iris.Context) {
	results := []map[string]interface{}{}
	utils.DB.Raw("select * from nav where isDelete = 1").Find(&results)
	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
	})

}
func (a Nav) GetObjectId(i iris.Context) {
	id, _ := i.Params().GetInt("id")

	fmt.Println(id)

	// page 当前页
	page, _ := i.URLParamInt("page")
	if page == -1 || page == 0 {
		page = 1
	}
	// pageSize 每页条数
	pageSize, _ := i.URLParamInt("pageSize")
	if pageSize == -1 {
		pageSize = 5
	}

	// 总条数
	var count string
	utils.DB.Raw("select count(*) from article WHERE nav_id = ?",id).Find(&count)

	fmt.Println(page, pageSize, count, "==========")
	results := []map[string]interface{}{}
	utils.DB.Debug().Raw("SELECT * FROM article WHERE nav_id = ? LIMIT ? OFFSET ?",id, pageSize, (page-1)*pageSize).Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
		"page": map[string]interface{}{
			"count":    count,
			"pageSize": pageSize,
			"page":     page,
		},
	})
}
